#!/usr/bin/env node

//通过return传递，若在M-logger和M-ms之间有别的return,需要一直return传递，
//面向过程
const Koa = require('koa');
const log = console.log;
const app = new Koa();
app.listen(8080);

app.use(async (ctx, next) => {  //M-logger
    log(`${ctx.method} ${ctx.path}`);
    const cost = await next();
    log(`cost: ${cost}ms`);
})

app.use((ctx, next) => {  //M-ms
    const start = Date.now();
    next();
    const end = Date.now();
    return end -start;
});

app.use((ctx, next) => {  //M-hello
    log('hello Koa!');
    ctx.body = 'ok';
})